package com.example.redis.myJredis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.ResourceBundle;

/**
 * Created by SOAL on 2017/6/2 0002.
 */
public class JredisPool {
    private static JedisPool pool;

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("application-dev");
        if (bundle == null) {
            throw new IllegalArgumentException("[redis.properties] is not found!");
        }
        System.out.println(bundle .getString("redis.pool.maxActive"));

        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
        config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
        config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
        config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
        pool = new JedisPool(config, bundle.getString("redis.ip")
                ,Integer.valueOf(bundle.getString("redis.port")));
    }

    public static void main(String[] args) {
    // 从池中获取一个Jedis对象
        Jedis jedis = pool.getResource();
        String keys = "name";

        // 删数据
        jedis.del(keys);
        // 存数据2
        jedis.set(keys, "snowolf2");
        // 取数据
        String value = jedis.get(keys);

        System.out.println(value);

        // 释放对象池
        pool.returnResource(jedis);

    }
}
